package com.hd.persistence.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.hd.common.DataPage;
import com.hd.common.HibernateEntityDao;
import com.hd.persistence.model.RepairReport;

public class RepairReportDao extends HibernateEntityDao<RepairReport> {
	
	public DataPage<RepairReport> pageQueryRepairReports(int pageNo, int pageSize) {
		String hql = new String("from RepairReport ");
		return pagedQuery(hql, pageNo, pageSize);
	}

	public void saveRepairReport(RepairReport repairReport) {
		getHibernateTemplate().saveOrUpdate(repairReport);
		getHibernateTemplate().flush();
	}
	
	public DataPage<RepairReport> pageQueryRepairReports(Date repairDate, String repairman, String unitName, Integer category, String machineNo, int pageNo, int pageSize) {
		StringBuilder hql = new StringBuilder("from RepairReport where (1=1) ");
		List<Object> objs = new ArrayList<Object>();
		if ( repairDate != null ) {
			hql.append("and repairDate=? ");
			objs.add(repairDate);
		}
		if ( StringUtils.isNotBlank(repairman) ) {
			hql.append("and repairman=? ");
			objs.add(repairman);
		}
		if ( StringUtils.isNotBlank(unitName) ) {
			hql.append("and unitName=? ");
			objs.add(unitName);
		}
		if ( category != null ) {
			hql.append("and category=? ");
			objs.add(category);
		}
		if ( StringUtils.isNotBlank(machineNo) ) {
			hql.append("and machineNo like ? ");
			objs.add("%" + machineNo + "%");
		}
		return pagedQuery(hql.toString(), pageNo, pageSize, "repairDate", false, objs.toArray());
	} 
}
